package com.jdd.modules.sdk;

import cn.hutool.core.util.ObjectUtil;
import com.jdd.common.api.vo.Result;
import com.jdd.common.constant.JddConstant;
import com.jdd.common.util.CacheUtil;
import com.jdd.modules.parking.entity.ConfigParkInfo;
import com.jdd.modules.parking.service.IConfigParkInfoService;
import com.jdd.modules.sdk.sdkcall.HuaXiaSdkCallInterface;
import com.jdd.modules.sdk.sdkcall.QianYiSdkCallInterface;
import com.jdd.modules.sdk.sdkcall.ZhenShiSdkCallInterface;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @version 1.0
 * @Description:
 * @author： jdd尚岳
 * @Date: 2021-07-29 17:26
 */
@Component
@Slf4j
public abstract class SdkCallInterface {
    @Resource
    private ZhenShiSdkCallInterface zhenShiSdkCallInterface;
    @Resource
    private QianYiSdkCallInterface qianYiSdkCallInterface;
    @Resource
    private HuaXiaSdkCallInterface huaXiaSdkCallInterface;
    @Resource
    private IConfigParkInfoService configParkInfoService;
    @Resource
    private CacheUtil cacheUtil;

    /**
     * @author lcy
     * @date 2021/8/16
     * @Description SDK初始化后，对相机的初始化
     */
    public Result CameraSdkInit(String ip, ConfigParkInfo configParkInfo) {
        Result result = null;
        if (ObjectUtil.isNull(configParkInfo)) {
            return Result.error("车辆信息配置为空，请检查");
        }
        if (JddConstant.CameraControl.CAMERA_ZHENSHI.equals(configParkInfo.getCameraType())) {
            result = zhenShiSdkCallInterface.CameraSdkInit(ip);
        }
        if (JddConstant.CameraControl.CAMERA_QIANYI.equals(configParkInfo.getCameraType())) {
            result = qianYiSdkCallInterface.CameraSdkInit(ip);
        }
        if (JddConstant.CameraControl.CAMERA_HUAXIA.equals(configParkInfo.getCameraType())) {
            result = huaXiaSdkCallInterface.CameraSdkInit(ip);
        }
        return result;
    }
    public Result Sdk485VoiceCommunicationAll(String serialNo, String str) {
        log.info("=====调用SDK语音播报======序列号{},文本{}",serialNo,str);
        Result result = null;
        ConfigParkInfo configParkInfo = getConfigParkInfo();
        if (ObjectUtil.isNull(configParkInfo)) {
            return Result.error("车辆信息配置为空，请检查");
        }
        if (JddConstant.CameraControl.CAMERA_ZHENSHI.equals(configParkInfo.getCameraType())) {
            result = zhenShiSdkCallInterface.Sdk485VoiceCommunication(serialNo, str);
        }
        if (JddConstant.CameraControl.CAMERA_QIANYI.equals(configParkInfo.getCameraType())) {
            result = qianYiSdkCallInterface.Sdk485VoiceCommunication(serialNo, str);
        }
        if (JddConstant.CameraControl.CAMERA_HUAXIA.equals(configParkInfo.getCameraType())) {
            result = huaXiaSdkCallInterface.Sdk485VoiceCommunication(serialNo, str);
        }
        log.info("=====调用SDK语音播报======结果->{}",result);
        return result;
    }

    public Result Sdk485TestContentCommunicationAll(String serialNo, String str, String like) {
        log.info("=====调用SDK临时文字播报======序列号{},文本{},行号{}",serialNo,str,like);

        Result result = null;
        ConfigParkInfo configParkInfo = getConfigParkInfo();
        if (ObjectUtil.isNull(configParkInfo)) {
            return Result.error("车辆信息配置为空，请检查");
        }
        if (JddConstant.CameraControl.CAMERA_ZHENSHI.equals(configParkInfo.getCameraType())) {
            result = zhenShiSdkCallInterface.Sdk485TestContentCommunication(serialNo, str, like);
        }
        if (JddConstant.CameraControl.CAMERA_QIANYI.equals(configParkInfo.getCameraType())) {
            result = qianYiSdkCallInterface.Sdk485TestContentCommunication(serialNo, str, like);
        }
        if (JddConstant.CameraControl.CAMERA_HUAXIA.equals(configParkInfo.getCameraType())) {
            result = huaXiaSdkCallInterface.Sdk485TestContentCommunication(serialNo, str, like);
        }

        return result;
    }

    /**
     * 广告指令
     * @param str
     * @return
     * @author renshangyi
     * @date 2022/1/15
     */
    public Result Sdk485LedAdvertising(String serialNo, String str, String like) {
        Result result = null;
        ConfigParkInfo configParkInfo = getConfigParkInfo();
        if (ObjectUtil.isNull(configParkInfo)) {
            return Result.error("车辆信息配置为空，请检查");
        }
        if (JddConstant.CameraControl.CAMERA_ZHENSHI.equals(configParkInfo.getCameraType())) {
            result = zhenShiSdkCallInterface.Sdk485LedAdvertising(serialNo, str, like);
        }
        if (JddConstant.CameraControl.CAMERA_QIANYI.equals(configParkInfo.getCameraType())) {
            result = qianYiSdkCallInterface.Sdk485LedAdvertising(serialNo, str, like);
        }
        if (JddConstant.CameraControl.CAMERA_HUAXIA.equals(configParkInfo.getCameraType())) {
            result = huaXiaSdkCallInterface.Sdk485LedAdvertising(serialNo, str, like);
        }
        return result;
    }

    /**
     *  方控：修改日期显示位置
     * @param   serialNo
     * @return
     * @author renshangyi
     * @date 2022/1/26
     */
    public Result Sdk485LedModifyDatePosition(String serialNo,String order) {
        Result result = null;
        ConfigParkInfo configParkInfo = getConfigParkInfo();
        if (ObjectUtil.isNull(configParkInfo)) {
            return Result.error("车辆信息配置为空，请检查");
        }
        if (JddConstant.CameraControl.CAMERA_ZHENSHI.equals(configParkInfo.getCameraType())) {
            result = zhenShiSdkCallInterface.Sdk485LedModifyDatePosition(serialNo,order);
        }
        if (JddConstant.CameraControl.CAMERA_QIANYI.equals(configParkInfo.getCameraType())) {
            result = qianYiSdkCallInterface.Sdk485LedModifyDatePosition(serialNo,order);
        }
        if (JddConstant.CameraControl.CAMERA_HUAXIA.equals(configParkInfo.getCameraType())) {
            result = huaXiaSdkCallInterface.Sdk485LedModifyDatePosition(serialNo,order);
        }
        return result;
    }

    /**
     * 取消临显内容
     * @param serialNo
     * @return
     * @author renshangyi
     * @date 2022/1/15
     */
    public Result Sdk485LedNoContentCommunication(String serialNo) {
        ConfigParkInfo configParkInfo = getConfigParkInfo();
        if (ObjectUtil.isNull(configParkInfo)) {
            return Result.error("车辆信息配置为空，请检查");
        }
        if (JddConstant.CameraControl.CAMERA_ZHENSHI.equals(configParkInfo.getCameraType())) {
            return zhenShiSdkCallInterface.Sdk485LedNoContentCommunication(serialNo);
        }
//        if (JddConstant.CameraControl.CAMERA_QIANYI.equals(configParkInfo.getCameraType())) {
//            return qianYiSdkCallInterface.Sdk485Calibration(serialNo);
//        }
//        if (JddConstant.CameraControl.CAMERA_HUAXIA.equals(configParkInfo.getCameraType())) {
//            return huaXiaSdkCallInterface.Sdk485Calibration(serialNo);
//        }
        return Result.error("序列号:" + serialNo + "取消临显内容出现问题");
    }

    /**
     *  启用遥控器开闸上传消息指令
     * @param serialNo
     * @return
     * @author shangyue
     * @date 2022/1/15
     */
    public Result Sdk485DaoZhaUploadMessage(String serialNo) {
        ConfigParkInfo configParkInfo = getConfigParkInfo();
        if (ObjectUtil.isNull(configParkInfo)) {
            return Result.error("车辆信息配置为空，请检查");
        }
        if (JddConstant.CameraControl.CAMERA_ZHENSHI.equals(configParkInfo.getCameraType())) {
            return zhenShiSdkCallInterface.Sdk485LedNoContentCommunication(serialNo);
        }
//        if (JddConstant.CameraControl.CAMERA_QIANYI.equals(configParkInfo.getCameraType())) {
//            return qianYiSdkCallInterface.Sdk485Calibration(serialNo);
//        }
//        if (JddConstant.CameraControl.CAMERA_HUAXIA.equals(configParkInfo.getCameraType())) {
//            return huaXiaSdkCallInterface.Sdk485Calibration(serialNo);
//        }
        return Result.error("序列号:" + serialNo + "启用遥控器开闸上传消息指令出现异常");
    }
    /**

    /**
     * 绿灯超时转红灯（0x13）
     * @param serialNo 序列号
     * @param order 绿灯超时多少秒转红灯
     * @return
     * @author renshangyi
     * @date 2022/3/21
     */
    public Result Sdk485LedGreenToRed(String serialNo,String order) {
        Result result = null;
        ConfigParkInfo configParkInfo = getConfigParkInfo();
        if (ObjectUtil.isNull(configParkInfo)) {
            return Result.error("车辆信息配置为空，请检查");
        }
        if (JddConstant.CameraControl.CAMERA_ZHENSHI.equals(configParkInfo.getCameraType())) {
            result = zhenShiSdkCallInterface.Sdk485LedGreenToRed(serialNo,order);
        }
        if (JddConstant.CameraControl.CAMERA_QIANYI.equals(configParkInfo.getCameraType())) {
            result = qianYiSdkCallInterface.Sdk485LedGreenToRed(serialNo,order);
        }
        if (JddConstant.CameraControl.CAMERA_HUAXIA.equals(configParkInfo.getCameraType())) {
            result = huaXiaSdkCallInterface.Sdk485LedGreenToRed(serialNo,order);
        }
        return result;
    }
    /*
     * @author lcy
     * @date 2021/8/16
     * @Description 通用开闸
     */
    public Result SdkUnfoldAll(String serialNo) {
        log.info("=====调用SDK开闸=====");
        Result result = null;
        ConfigParkInfo configParkInfo = getConfigParkInfo();
        if (ObjectUtil.isNull(configParkInfo)) {
            return Result.error("车辆信息配置为空，请检查");
        }
        if (JddConstant.CameraControl.CAMERA_ZHENSHI.equals(configParkInfo.getCameraType())) {
            result = zhenShiSdkCallInterface.SdkUnfold(serialNo);
        }
        if (JddConstant.CameraControl.CAMERA_QIANYI.equals(configParkInfo.getCameraType())) {
            result = qianYiSdkCallInterface.SdkUnfold(serialNo);
        }
        if (JddConstant.CameraControl.CAMERA_HUAXIA.equals(configParkInfo.getCameraType())) {
            result = huaXiaSdkCallInterface.SdkUnfold(serialNo);
        }
        log.info("=====调用SDK开闸日志=====->{}",result.getMessage());
        return result;
    }

    /**
     * @author lcy
     * @date 2021/8/16
     * @Description 带牌照的通用开闸
     */
    public Result SdkUnfoldAll(String swichType, String serialNo, String plateNo) {
        log.info("=====调用SDK开闸日志======开闸类型:{},序列号:{}0,车牌号:{}",swichType,serialNo,plateNo);
        Result result = null;
        ConfigParkInfo configParkInfo = getConfigParkInfo();
        if (ObjectUtil.isNull(configParkInfo)) {
            return Result.error("车辆信息配置为空，请检查");
        }
        if (JddConstant.CameraControl.CAMERA_ZHENSHI.equals(configParkInfo.getCameraType())) {
            result = zhenShiSdkCallInterface.SdkUnfold(swichType, serialNo, plateNo);
        }
        if (JddConstant.CameraControl.CAMERA_QIANYI.equals(configParkInfo.getCameraType())) {
            result = qianYiSdkCallInterface.SdkUnfold(swichType, serialNo, plateNo);
        }
        if (JddConstant.CameraControl.CAMERA_HUAXIA.equals(configParkInfo.getCameraType())) {
            result = huaXiaSdkCallInterface.SdkUnfold(swichType, serialNo, plateNo);
        }
        log.info("=====调用SDK开闸日志======结果->{}",result);
        return result;
    }

    /**
     * @author lcy
     * @date 2021/8/13
     * @Description 通用查看是否在线
     */
    public Result IsConnectAll(String serialNo) {
        Result result = null;
        ConfigParkInfo configParkInfo = getConfigParkInfo();
        if (ObjectUtil.isNull(configParkInfo)) {
            return Result.error("车辆信息配置为空，请检查");
        }
        if (JddConstant.CameraControl.CAMERA_ZHENSHI.equals(configParkInfo.getCameraType())) {
            result = zhenShiSdkCallInterface.GetIsConnect(serialNo);
        }
        if (JddConstant.CameraControl.CAMERA_QIANYI.equals(configParkInfo.getCameraType())) {
            result = qianYiSdkCallInterface.GetIsConnect(serialNo);
        }
        if (JddConstant.CameraControl.CAMERA_HUAXIA.equals(configParkInfo.getCameraType())) {
            result = huaXiaSdkCallInterface.GetIsConnect(serialNo);
        }
        return result;
    }

    /**
     * 软触发
     *
     * @param serialNo
     * @return com.jdd.common.api.vo.Result
     * @author jdd孙庆伟
     * @date 2021/8/31 10:51:41
     * @version 1.0
     */
    public Result TriggerExtAll(String serialNo) {
        Result result = null;
        ConfigParkInfo configParkInfo = getConfigParkInfo();
        if (ObjectUtil.isNull(configParkInfo)) {
            return Result.error("车辆信息配置为空，请检查");
        }
        if (JddConstant.CameraControl.CAMERA_ZHENSHI.equals(configParkInfo.getCameraType())) {
            result = zhenShiSdkCallInterface.TriggerExt(serialNo);
        }
        if (JddConstant.CameraControl.CAMERA_QIANYI.equals(configParkInfo.getCameraType())) {
            result = qianYiSdkCallInterface.TriggerExt(serialNo);
        }
        if (JddConstant.CameraControl.CAMERA_HUAXIA.equals(configParkInfo.getCameraType())) {
            result = huaXiaSdkCallInterface.TriggerExt(serialNo);
        }
        return result;
    }
    /*** 功能描述: 菲力德同步时间
     * @Author: lcy
     * @Date: 2022/1/4
     */
    public Result calibrationAll(String serialNo) {
        ConfigParkInfo configParkInfo = getConfigParkInfo();
        if (ObjectUtil.isNull(configParkInfo)) {
            return Result.error("车辆信息配置为空，请检查");
        }
        if (JddConstant.CameraControl.CAMERA_ZHENSHI.equals(configParkInfo.getCameraType())) {
            return zhenShiSdkCallInterface.Sdk485Calibration(serialNo);
        }
        if (JddConstant.CameraControl.CAMERA_QIANYI.equals(configParkInfo.getCameraType())) {
            return qianYiSdkCallInterface.Sdk485Calibration(serialNo);
        }
        if (JddConstant.CameraControl.CAMERA_HUAXIA.equals(configParkInfo.getCameraType())) {
            return huaXiaSdkCallInterface.Sdk485Calibration(serialNo);
        }
        return Result.error("序列号:"+serialNo+"同步时间出现问题");
    }
    /**
     * @author lcy
     * @date 2021/8/13
     * @Description 拿到当前相机
     */
    private ConfigParkInfo getConfigParkInfo() {
        //这里不作null判断，调用的方法做判断
        return configParkInfoService.findConfigParkInfoByParkCode();
    }

    /*** 功能描述:判断月租车还是白名单过期
     */
    public String getOverdueType(String plateNo) {
        ConfigParkInfo configParkInfo = getConfigParkInfo();
        String overdueType = cacheUtil.get("parkToEnd", plateNo);
        if (StringUtils.isBlank(overdueType)) {
            return "";
        }
        if (JddConstant.OverdueType.MONTH_OVERDUE.equals(overdueType)) {
            //菲利德
            if (JddConstant.CameraControl.CONTROLPANEL_FEILIDE.equals(configParkInfo.getControlPanelType())) {
                return "月卡车,已过期,按临时车计费,";
            }
            //芊熤
            if (JddConstant.CameraControl.CONTROLPANEL_QIANYI.equals(configParkInfo.getControlPanelType())) {
                return "月卡车,已过期,按临时车计费,";
            }
            //启协
            if (JddConstant.CameraControl.CONTROLPANEL_QIXIE.equals(configParkInfo.getControlPanelType())) {
                return "月租车,已过期,按临时车计费,";
            }
            //方控
            if (JddConstant.CameraControl.CONTROLPANEL_FANGKONG.equals(configParkInfo.getControlPanelType())) {
                return "月租车,已过期,按临时车计费,";
            }
            //小明
            if (JddConstant.CameraControl.CONTROLPANEL_XIAOMING.equals(configParkInfo.getControlPanelType())) {
                return "月租车,卡已过期,按临时车计费,";
            }
            //小竖屏
            if (JddConstant.CameraControl.CONTROLPANEL_SMALL_FANGKONG.equals(configParkInfo.getControlPanelType())) {
                return "月租车,"+plateNo+",卡已过期,按临时车计费,";
            }
            // YZL 绿卡
            if (JddConstant.CameraControl.CONTROLPANEL_YANGZHONGLAI_GREEN.equals(configParkInfo.getControlPanelType())) {
                return "月卡车" +plateNo+ ",已过期,出场为临时车收费";
            }
            // YZL 蓝卡
            if (JddConstant.CameraControl.CONTROLPANEL_YANGZHONGLAI_BLUE.equals(configParkInfo.getControlPanelType())) {
                return "月卡车," +plateNo+ ",已过期,出场为临时车收费";
            }
            // 安快
            if (JddConstant.CameraControl.CONTROLPANEL_ANKUAI.equals(configParkInfo.getControlPanelType())) {
                return "月租车," +plateNo+ ",已过期,为,临时车";
            }
        }
        //小明
        if (JddConstant.CameraControl.CONTROLPANEL_XIAOMING.equals(configParkInfo.getControlPanelType())) {
            return "免费车,卡已过期,按临时车计费,";
        }
        //方控
        if (JddConstant.CameraControl.CONTROLPANEL_FANGKONG.equals(configParkInfo.getControlPanelType())) {
            return "白名单,已过期,按临时车计费,";
        }
        //芊熤
        if (JddConstant.CameraControl.CONTROLPANEL_QIANYI.equals(configParkInfo.getControlPanelType())) {
            return "月卡车,已过期,按临时车计费,";
        }
        //启协
        if (JddConstant.CameraControl.CONTROLPANEL_QIXIE.equals(configParkInfo.getControlPanelType())) {
            return "固定车,已过期,按临时车计费,";
        }
        //菲利德
        if (JddConstant.CameraControl.CONTROLPANEL_FEILIDE.equals(configParkInfo.getControlPanelType())) {
            return "固定车,已过期,";
        }
        //小竖屏
        if (JddConstant.CameraControl.CONTROLPANEL_SMALL_FANGKONG.equals(configParkInfo.getControlPanelType())) {
            return "免费车,"+plateNo+",此车已过期,按临时车计费,";
        }
        // YZL 绿卡
        if (JddConstant.CameraControl.CONTROLPANEL_YANGZHONGLAI_GREEN.equals(configParkInfo.getControlPanelType())) {
            return "固定车" +plateNo+ ",已过期,出场为临时车收费";
        }
        // YZL 蓝卡
        if (JddConstant.CameraControl.CONTROLPANEL_YANGZHONGLAI_BLUE.equals(configParkInfo.getControlPanelType())) {
            return "固定车," +plateNo+ ",已过期,出场为临时车收费";
        }
        // 安快
        if (JddConstant.CameraControl.CONTROLPANEL_ANKUAI.equals(configParkInfo.getControlPanelType())) {
            return "会员车," +plateNo+ ",已过期,为,临时车";
        }
        return null;
    }


    public abstract Result SdkUnfold(String serialNo);

    public abstract Result SdkUnfold(String swichType, String serialNo, String plateNo);

    public abstract Result CameraSdkInit(String ip);

    public abstract Result Sdk485VoiceCommunication(String serialNo, String str);

    public abstract Result Sdk485TestContentCommunication(String serialNo, String str, String like);

    public abstract Result GetIsConnect(String serialNo);

    public abstract Result TriggerExt(String serialNo);


}
